package cn.com.dhc.danlu.business.shopcar.popup;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.widget.DLToolbar;
import cn.com.dhc.danlu.widget.base.BasePopupWindow;
import cn.com.hd.mall.web.webservices.entity.response.shoppingcart.ToShoppingcartResponse.SellerListBean.MerchListBean.PromotionModelListBean;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-10 11:34
 *
 * <p>
 * 选择使用优惠的弹窗
 * </p>
 */
public class PromotionSelectPopup extends BasePopupWindow implements View.OnClickListener, PromotionSelectAdapter.OnSelectListener {
    private View popView;
    private XRecyclerView xrvPromotions;
    private int promotionSelIndex;

    public PromotionSelectPopup(Context context, ViewGroup rootView, List<PromotionModelListBean> entities, int lastSelIndex) {
        super(context);
        initSelf(context, rootView);
        initChildViews();
        setChildViews(context, entities, lastSelIndex);
        setContentView(popView);
    }

    private void initSelf(Context context, ViewGroup rootView) {
        popView = LayoutInflater.from(context).inflate(R.layout.popview_promotion_select, rootView, false);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setAnimationStyle(R.style.popwin_anim_style);
    }

    private void initChildViews() {
        popView.findViewById(R.id.v_empty).setOnClickListener(this);
        ((DLToolbar) popView.findViewById(R.id.dl_toolbar)).setRightClickListenter(this);

        xrvPromotions = (XRecyclerView) popView.findViewById(R.id.xrv_promotions);
        xrvPromotions.setLayoutManager(new LinearLayoutManager(xrvPromotions.getContext()));
        xrvPromotions.setHasFixedSize(true);
        xrvPromotions.setLoadingMoreEnabled(false);
        xrvPromotions.setPullRefreshEnabled(false);
    }

    private void setChildViews(Context context, List<PromotionModelListBean> entities, int lastSelIndex) {
        // TODO: 2017/3/10 传入上一次选中的按钮 @lyf #ok
        PromotionSelectAdapter adapter = new PromotionSelectAdapter(context, lastSelIndex);
        xrvPromotions.setAdapter(adapter);
        adapter.refreshAllData(entities);
        adapter.setOnSelectListener(this);

        if (lastSelIndex == entities.size()) {
            setPromotionSelIndex(entities.size());
        } else {
            setPromotionSelIndex(lastSelIndex);
        }
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    public int getPromotionSelIndex() {
        return promotionSelIndex;
    }

    private void setPromotionSelIndex(int promotionSelIndex) {
        this.promotionSelIndex = promotionSelIndex;
    }

    @Override
    public void onSelect(View v, int selectIndex) {
        setPromotionSelIndex(selectIndex);

        v.postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        }, 200);
    }
}
